package com.yisu.auction.api.core.service.impl;

import com.yisu.auction.api.core.mapper.AddressMapper;
import com.yisu.auction.api.core.service.IAddressService;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.memberPackage.AddressRequest;
import com.yisu.auction.api.domain.request.prizePackage.ReceivingAddressRequest;
import com.yisu.auction.api.domain.vo.PrizePackageVos.ReceivingAddressVo;
import com.yisu.auction.ewf.entity.Address;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/8/19
 */
@Service("IAddressService")
public class AddressServiceImpl implements IAddressService {

    @Resource
    private AddressMapper addressMapper;

    /**
     * author:黄萧林
     * Business Description:添加收货地址
     *
     * @param receivingAddressRequest
     * @return
     */
    @Override
    public boolean addReceivingAddress(ReceivingAddressRequest receivingAddressRequest) {
        Address address = new Address();
        address.setCreateDate(new Date());
        address.setIsValid(true);
        address.setMemberId(Long.valueOf(receivingAddressRequest.getMemberId()));
        address.setLinker(receivingAddressRequest.getLinker());
        address.setMobile(receivingAddressRequest.getMobile());
        address.setProvince(receivingAddressRequest.getProvince());
        address.setRegion(receivingAddressRequest.getRegion());
        address.setCity(receivingAddressRequest.getCity());
        address.setCountry(receivingAddressRequest.getCountry());
        address.setDetail(receivingAddressRequest.getDetail());
        address.setQq(receivingAddressRequest.getQq());
        address.setAlipay(receivingAddressRequest.getAlipay());
        address.setIsDefualt(receivingAddressRequest.getIsDefualt());
        if (address.getIsDefualt() == true) {
            if (addressMapper.cancelDefaultAddressByMemberId(address.getMemberId()) > 0) {
                return addressMapper.insertSelective(address) > 0;
            }
        }
        return addressMapper.insertSelective(address) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:获取当前收货地址信息
     *
     * @param id
     * @return
     */
    @Override
    public ReceivingAddressVo getReceivingAddressInfo(Long id) {
        return addressMapper.getReceivingAddressInfo(id);
    }

    /**
     * author:黄萧林
     * Business Description:删除收货地址
     *
     * @param addressRequest 含memberId,id
     * @return
     */
    @Override
    public Boolean deleteReceivingAddress(AddressRequest addressRequest) {
        return addressMapper.deleteByMemberIdAndPrimary(addressRequest) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:保存当前修改数据
     *
     * @param receivingAddressVo
     * @return
     */
    @Override
    public Boolean saveReceivingAddressInfo(ReceivingAddressVo receivingAddressVo) {
        Address address = new Address();
        address.setId(receivingAddressVo.getId());
        address.setUpdateDate(new Date());
        address.setMemberId(Long.valueOf(receivingAddressVo.getMemberId()));
        address.setLinker(receivingAddressVo.getLinker());
        address.setMobile(receivingAddressVo.getMobile());
        address.setProvince(receivingAddressVo.getProvince());
        address.setCity(receivingAddressVo.getCity());
        address.setRegion(receivingAddressVo.getRegion());
        address.setCountry(receivingAddressVo.getCountry());
        address.setDetail(receivingAddressVo.getDetail());
        address.setAlipay(receivingAddressVo.getAlipay());
        address.setQq(receivingAddressVo.getQq());
        address.setIsDefualt(receivingAddressVo.getIsDefualt());
        if (receivingAddressVo.getIsDefualt() == true) {
            if (addressMapper.cancelDefaultAddressByMemberId(address.getMemberId()) > 0) {
                return addressMapper.updateByPrimaryKeySelective(address) > 0;
            }
        }
        return addressMapper.updateByPrimaryKeySelective(address) > 0;
    }

    /**
     * author:黄萧林
     * Business Description:获取我的收货地址列表
     *
     * @param basePage
     * @return
     */
    @Override
    public List<ReceivingAddressVo> getMineReceivingAddressList(BasePage basePage) {
        return addressMapper.getMineReceivingAddressList(basePage);
    }

    /**
     * author:赵杰
     * Business Description: 通过会员ID获取默认收货地址
     *
     * @param memberId
     * @return
     */
    @Override
    public ReceivingAddressVo getDefaultAddressByMemberId(Long memberId) {
        return addressMapper.getDefaultAddressByMemberId(memberId);
    }

}
